/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @class: Wg
 * 
 * */
function Wg(J) {
    this.json = J;
    this.ls_concap = [];
    var $W = this;

    this.init = function () {
        $.each(this.json.ls_concap, function (i, concap) {
            $W.ls_concap[$W.ls_concap.length] = new Concap(concap);
        });
    };

    this.getConcap = function (id) {
        return this.ls_concap[id];
    };

    this.init();
}

/**
 * @class Concap
 * */
function Concap(J) {
    this.json = J;
    this.co_concap = J.co_concap;
    this.il_visible = J.il_visibl;
    this.no_concap = J.no_concap;
    this.ls_entgeo = [];
    var $C = this;

    this.init = function () {
        $.each(this.json.ls_entgeo, function (i, entgeo) {
            $C.ls_entgeo[$C.ls_entgeo.length] = new Entgeo(entgeo, $C);
        });
    };

    this.setVisible = function (V) {
        $C.il_visible = V;
        $.each($C.ls_entgeo, function (i, entgeo) {
            entgeo.setVisible($C.il_visible);
        });
    };

    this.isVisible = function () {
        return $C.il_visible;
    };

    this.init();
}

/**
 * @class Entgeo
 * */
function Entgeo(J, concap) {
    this.json = J;
    this.co_entgeo = J.co_entgeo;
    this.no_entgeo = J.no_entgeo;
    this.il_visible = J.il_visibl;
    this.ti_entgeo = J.ti_entgeo;
    this.va_autref = J.va_autref;
    this.ls_grafos = [];
    this.intervalo = null;
    this.concap = concap;

    var $E = this;

    this.init = function () {
        $.each(this.json.ls_grafos, function (i, grafo) {
            $E.ls_grafos[$E.ls_grafos.length] = new Grafo(grafo, $E);
        });
        $E.startTimer();
    };

    this.refresh = function () {
//        $.getJSON("wgp?xact=12&p_co_entgeo=" + $E.co_entgeo + "&p_id_frawor=" + ID_FRAWOR + "&p_co_congeo=" + CO_CONGEO + "&p_ti_entgeo=" + $E.ti_entgeo, function(entgeo) {
        $.getJSON("../wgp?xact=12&p_co_entgeo=" + $E.co_entgeo + "&p_id_frawor=" + ID_FRAWOR + "&p_co_congeo=" + CO_CONGEO + "&p_ti_entgeo=" + $E.ti_entgeo, function (entgeo) {
            $.each(entgeo.ls_grafos, function (i, grafo) {
                $.each($E.ls_grafos, function (o, gf) {
                    if (grafo.id_grafo === gf.id) {
                        $E.ls_grafos[o].refresh(grafo);
                    }
                });
            });
            $E.intervalo = setTimeout($E.refresh, $E.va_autref * 1000);
        });
    };

    this.setVisible = function (V) {
        $E.il_visible = V;
        $.each($E.ls_grafos, function (i, grafo) {
            grafo.setVisible($E.il_visible);
        });
    };

    this.isVisible = function () {
        return $E.il_visible;
    };

    this.stopTimer = function () {
        clearTimeout($E.intervalo);
    };

    this.startTimer = function (T) {
        if (T)
            $E.va_autref = T;
        if ($E.va_autref > 0) {
            $E.intervalo = setTimeout($E.refresh, $E.va_autref * 1000);
        } else {
            console.log("Valor de autorefresco invalido.");
        }
    };

    this.init();
}

/**
 * @class Grafo
 * */

function Grafo(J, entgeo) {
    this.json = J;
    this.id = J.id_grafo;

    this.entgeo = entgeo;
    this.il_visible = !this.entgeo.concap.il_visible ? false : this.entgeo.il_visible ? true : false;

    this.grafo = null;
    this.label = null;

    var $G = this;

    this.click_me = function () {
        infocredito2($G.grafo);
    };

    this.in_me = function () {
        pmouseover2($G.grafo);
    };

    this.out_me = function () {
        pmouseout2($G.grafo);
    };

    this.init = function () {
        this.obteniendoVariables();

        if (this.ti_entgeo == 'P') {
            this.grafo = new google.maps.Marker({
                position: new google.maps.LatLng(this.json.ls_puntos[0].nu_geolat, this.json.ls_puntos[0].nu_geolon),
                map: this.il_visible ? map : null,
                draggable: false,
                descripcion: this.no_entdes,
                animation: google.maps.Animation.DROP
            });

            if (this.ti_entgra > 1) {
                if (this.ti_entgra == 2)
//                    this.grafo.setIcon(new google.maps.MarkerImage('img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(15, 15)));
                    this.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(15, 15)));
                if (this.ti_entgra == 4)
//                    this.grafo.setIcon(new google.maps.MarkerImage('img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(12, 12)));
                    this.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(12, 12)));
                else
//                    this.grafo.setIcon(new google.maps.MarkerImage('img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(25, 25), google.maps.Size(50, 50)));
                    this.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(25, 25), google.maps.Size(50, 50)));
            }
//                this.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol, null, null, new google.maps.Point(25, 25), google.maps.Size(50, 50)));


//            console.log('img?xact=' + this.ti_entgra + '&nu_angulo=' + this.nu_entdir + '&no_color=' + this.co_entcol);
//            console.log("->grafo inicial>>" + this.grafo.getPosition().lat() + "::" + this.grafo.getPosition().lng());

            if (this.no_entdes != undefined | this.no_entdes != null)
                google.maps.event.addListener(this.grafo, 'click', infocredito);
            //generar nombre
//            this.label = new MapLabel({
//                text: this.no_entnom,
//                position: new google.maps.LatLng(this.json.ls_puntos[0].nu_geolat, this.json.ls_puntos[0].nu_geolon),
//                map: this.il_visible ? map : null,
//                fontSize: 15,
//                align: 'center',
//                valign: 'bottom'
//            });

        } else if (this.ti_entgeo == 'A') {
            var path = this.create_path();

            this.grafo = new google.maps.Polygon({
                paths: path,
                strokeColor: this.co_entcol,
                strokeOpacity: (this.ti_entgra == 1) ? 0 : 0.5,
                strokeWeight: 1,
                fillColor: this.co_entcol,
                fillOpacity: 0.25,
                cursor: 'hand',
                map: this.il_visible ? map : null,
                descripcion: this.no_entdes
            });

            var bounds = new google.maps.LatLngBounds();

            for (var p = 0; p < path.length; p++) {
                bounds.extend(path[p]);
            }

            // generar descripción
            if (this.no_entdes != undefined | this.no_entdes != null)
                google.maps.event.addListener(this.grafo, 'click', infocredito);
            //generar nombre
            this.label = new MapLabel({
                text: this.no_entnom,
                position: bounds.getCenter(),
                map: this.il_visible ? map : null,
                fontSize: 15,
                align: 'center',
                valign: 'bottom'
            });

        } else if (this.ti_entgeo == 'L') {
//            console.log("creamdo linea: ");
//            console.log((this.co_entcol != null && this.co_entcol != undefined) ? this.co_entcol : 'black');
            var path = this.create_path();
            this.grafo = new google.maps.Polyline({
                path: path,
                strokeColor: (this.co_entcol != null && this.co_entcol != undefined) ? this.co_entcol : 'black',
                strokeOpacity: this.ti_entgra < 6 ? 0.7 : 0,
//                fillColor: (this.co_entcol != null && this.co_entcol != undefined) ? this.co_entcol : 'black',
//                fillOpacity: 1,
//                icons: this.create_icon(0.8),this.no_entdes
                icons: create_icon(this.ti_entgra, 0.7),
//                descripcion: this.no_entdes != undefined ? this.no_entdes : '',
                descripcion: this.no_entdes,
                nombre: this.no_entnom != undefined ? this.no_entnom : '',
                type: this.ti_entgra,
                clickable: true,
                strokeWeight: 2,
                map: this.il_visible ? map : null
            });
            this.grafo.getCenter = function () {
                if (path.length > 2) {
                    if (path.length % 2 == 0) {
                        //par
                        return path[path.length / 2];
                    } else {
                        //inpar
                        return path[(path.length + 1) / 2];
                    }
                } else {
                    return path[0];
                }
            };
            if (this.no_entdes != undefined | this.no_entdes != null)
                google.maps.event.addListener(this.grafo, 'click', infocredito);
//            google.maps.event.addListener(this.grafo, 'mouseover', function(event){
//                self.
//            });
            google.maps.event.addListener(this.grafo, 'mouseover', pmouseover);
            google.maps.event.addListener(this.grafo, 'mouseout', pmouseout);
//            google.maps.event.addListener(this.grafo, 'click', infocredito);


//            var request = {
//                origin:  path[0],
//                destination: path[path.length - 1],
//                travelMode: google.maps.TravelMode.DRIVING
//            };
//            directionsService.route(request, function(result, status) {
//                if (status == google.maps.DirectionsStatus.OK) {
//                    directionsDisplay.setDirections(result);
//                }
//            });

        } else if (this.ti_entgeo == 'C') {

            this.grafo = new google.maps.Circle({
                center: new google.maps.LatLng(this.json.ls_puntos[0].nu_geolat, this.json.ls_puntos[0].nu_geolon),
                radius: this.nu_georad,
                strokeColor: this.co_entcol,
                strokeOpacity: 1.0,
                strokeWeight: .9,
                fillColor: this.co_entcol,
                fillOpacity: 0.20,
                descripcion: this.no_entdes,
                map: this.il_visible ? map : null
            });
            if (this.no_entdes != undefined | this.no_entdes != null)
                google.maps.event.addListener(this.grafo, 'click', infocredito);
        }
    };

    this.refresh = function (jsn) {
//        console.log("refrescando "+this.ti_entgeo);
        $G.json = jsn;
        switch ($G.json.ti_entgeo) {
            case "P":
                var latlng = new google.maps.LatLng($G.json.ls_puntos[0].nu_geolat, $G.json.ls_puntos[0].nu_geolon);
                if (($G.grafo.getPosition().lat() !== latlng.lat()) | ($G.grafo.getPosition().lng() !== latlng.lng())) {
                    $G.grafo.setPosition(latlng);
//                    console.log("Nueva posicion");
                }
                if ($G.ti_entgra !== $G.json.ti_entgra | $G.nu_entdir !== $G.json.nu_entdir | $G.co_entcol !== $G.json.co_entcol) {
                    if ($G.ti_entgra === 2)
//                        $G.grafo.setIcon(new google.maps.MarkerImage('img?xact=' + $G.ti_entgra + '&nu_angulo=' + $G.nu_entdir + '&no_color=' + $G.co_entcol, null, null, new google.maps.Point(15, 15)));
                        $G.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + $G.ti_entgra + '&nu_angulo=' + $G.nu_entdir + '&no_color=' + $G.co_entcol, null, null, new google.maps.Point(15, 15)));
                    if ($G.ti_entgra === 4)
//                        $G.grafo.setIcon(new google.maps.MarkerImage('img?xact=' + $G.ti_entgra + '&nu_angulo=' + $G.nu_entdir + '&no_color=' + $G.co_entcol, null, null, new google.maps.Point(12, 12)));
                        $G.grafo.setIcon(new google.maps.MarkerImage('../img?xact=' + $G.ti_entgra + '&nu_angulo=' + $G.nu_entdir + '&no_color=' + $G.co_entcol, null, null, new google.maps.Point(12, 12)));
//                    console.log('img?xact=' + $G.ti_entgra + '&nu_angulo=' + $G.nu_entdir + '&no_color=' + $G.co_entcol);
//                    console.log("Nuevo marker");
                }
                if ($G.no_entdes !== $G.json.no_entdes) {
                    $G.grafo.setOptions({descripcion: $G.no_entdes});
//                    console.log("Nueva descipcion");
                }
                this.obteniendoVariables();
                break;
            case "A":
                this.obteniendoVariables();
                $G.grafo.setPaths($G.create_path());
                break;
            case "L":
                this.obteniendoVariables();
                $G.grafo.setPath($G.create_path());
                break;
            case "C":
                this.obteniendoVariables();
                $G.grafo.setCenter(new google.maps.LatLng($G.ls_puntos[0].nu_geolat, $G.ls_puntos[0].nu_geolon));
                $G.grafo.setRadius($G.nu_georad);
                break;
        }
    };

    this.create_path = function () {
        var data = [];
        for (var i = 0; i < this.ls_puntos.length; i++) {
            data[data.length] = new google.maps.LatLng(this.ls_puntos[i].nu_geolat, this.ls_puntos[i].nu_geolon);
        }
        return data;
    };

//    this.create_icon = function(p_stroke) {

    this.refrescarVisibilidad = function () {
        var visible = !this.entgeo.concap.il_visible ? false : this.entgeo.il_visible ? true : false;
        if (this.il_visible != visible) {
            this.il_visible = visible;
            this.grafo.setMap(this.il_visible ? map : null);
        }
//        console.log("refrescando visibilidad: "+this.ti_entgeo+" :: "+this.il_visible);
    };

    this.setVisible = function (visible) {
        if (this.il_visible != visible) {
            this.il_visible = visible;
            this.grafo.setMap(this.il_visible ? map : null);
        }
    };

    this.obteniendoVariables = function () {
        this.ti_entgeo = this.json.ti_entgeo;
        this.co_entcol = this.json.co_entcol;
        this.no_entnom = this.json.no_entnom;
        this.no_entdes = this.json.no_entdes;
        this.ti_entgra = this.json.ti_entgra;
        this.nu_georad = this.json.nu_georad;
        this.nu_entdir = this.json.nu_entdir;
        this.ls_puntos = this.json.ls_puntos;
    };

    this.init();


}
